package xw.qxypt.modules.baseinfo.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.Query;

import xw.qxypt.modules.baseinfo.dao.LhpDataVisibilityDao;
import xw.qxypt.modules.baseinfo.entity.LhpDataVisibilityEntity;
import xw.qxypt.modules.baseinfo.entity.LhpInstallationPointEntity;
import xw.qxypt.modules.baseinfo.service.LhpDataVisibilityService;
import xw.qxypt.modules.baseinfo.service.LhpInstallationPointService;
import xw.qxypt.modules.sys.shiro.ShiroUtils;


@Service("lhpDataVisibilityService")
public class LhpDataVisibilityServiceImpl extends ServiceImpl<LhpDataVisibilityDao, LhpDataVisibilityEntity> implements LhpDataVisibilityService {

    @Autowired
    LhpInstallationPointService lhpInstallationPointService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String pointName = (String)params.get("pointName");
        String startTime = (String)params.get("startTime");
        if(!StringUtils.isNotBlank(startTime)){
            startTime = sdf.format(new Date())+" 00:00:00";
        }
        String endTime = (String)params.get("endTime");
        if(StringUtils.isNotBlank(endTime)){
            endTime = endTime+" 23:59:59";
        }
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
        }
        if(pointName != null && !"".equals(pointName)){
            pIdList = new ArrayList<>();
            for(LhpInstallationPointEntity p:pointList){
                if(p.getPointName().indexOf(pointName) != -1){
                    pIdList.add(p.getId());
                }
            }
        }
        if(pIdList.size()==0){
            pIdList.add("safsedfgdgfdgfdg");
        }
        IPage<LhpDataVisibilityEntity> page = this.page(
                new Query<LhpDataVisibilityEntity>().getPage(params),
                new QueryWrapper<LhpDataVisibilityEntity>()
                .in(pIdList.size()>0,"point_id",pIdList)
                        .ge(StringUtils.isNotBlank(startTime),"data_time",startTime)
                        .le(StringUtils.isNotBlank(endTime),"data_time",endTime)
                .orderByDesc("data_time")
        );
        List<LhpDataVisibilityEntity> infoList = page.getRecords();
        for(LhpDataVisibilityEntity ldve:infoList){
            for(LhpInstallationPointEntity lipe:pointList){
                if(ldve.getPointId().equals(lipe.getId())){
                    ldve.setPointName(lipe.getPointName());
                    break;
                }
            }
        }

        return new PageUtils(page);
    }

    @Override
    public Map visibilityChart() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -10);
        //查询该用户下的所属安装点id
        //根据用户id查询关联的安装点
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        List<String> pNameList = new ArrayList<>();
        List<String> visiList = new ArrayList<>();
        Map<String,LhpDataVisibilityEntity> map1 = new HashMap<>();
        Map<String,LhpInstallationPointEntity> map2 = new HashMap<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
            map2.put(p.getId(), p);
        }
        LhpDataVisibilityEntity minVisi = new LhpDataVisibilityEntity();
        List<LhpDataVisibilityEntity> VisibilityList = this.list(
                new QueryWrapper<LhpDataVisibilityEntity>()
                        .gt("data_time",c.getTime())
                        .in("point_id",pIdList)
        );
        if(VisibilityList.size()>0) {
            for (LhpDataVisibilityEntity lhpdv : VisibilityList) {
                map1.put(lhpdv.getPointId(), lhpdv);
            }
            Collections.sort(VisibilityList,new Comparator<LhpDataVisibilityEntity>() {
                @Override
                public int compare(LhpDataVisibilityEntity o1, LhpDataVisibilityEntity o2) {
                    if(Double.parseDouble(o1.getVisibility())>=Double.parseDouble(o2.getVisibility())) {
                        return 1;
                    }else{
                        return -1;
                    }
                }
            });
            minVisi = VisibilityList.get(0);
            for (String  s : pIdList) {
                if(map1.get(s) != null) {
                    visiList.add(map1.get(s).getVisibility());
                    pNameList.add(map2.get(s).getPointName());
                }
            }
        }
        Map map=new HashMap();
        map.put("pNameList",pNameList);
        map.put("visiList", visiList);
        map.put("minVisi", minVisi);
        return map;
    }

    //根据用户查询每个安装点最后一条信息用于首页能见度图层
    @Override
    public List<LhpDataVisibilityEntity> visibilityOverlay() {
        //根据用户id查询关联的安装点
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
        }
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -6);
        List<LhpDataVisibilityEntity> visibilityList = this.list(
                new QueryWrapper<LhpDataVisibilityEntity>()
                        .gt("data_time",c.getTime())
                        .in("point_id",pIdList)
        );
        return visibilityList;
    }

    @Override
    public LhpDataVisibilityEntity getDataVisibilityByPointId(String pointId) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -15);
        List<LhpDataVisibilityEntity> visibilityList = this.list(
                new QueryWrapper<LhpDataVisibilityEntity>()
                        .gt("data_time",c.getTime())
                        .eq("point_id",pointId)
                        .orderByDesc("data_time")
        );
        if(visibilityList.size()>0){
            return visibilityList.get(0);
        }
        return null;
    }

    @Override
    public List<LhpDataVisibilityEntity> visibilityDataChart(String pointId) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR, -3);
        List<LhpDataVisibilityEntity> visibilityList = this.list(
                new QueryWrapper<LhpDataVisibilityEntity>()
                        .gt("data_time",c.getTime())
                        .eq("point_id",pointId)
                        .orderByAsc("data_time")
        );
        return visibilityList;
    }

    @Override
    public List<LhpDataVisibilityEntity> queryVisibilityDataByRoleId(Date startTime, String roleId) {
        return this.baseMapper.queryVisibilityDataByRoleId(startTime,roleId);
    }

}
